const router = require('koa-router')()
const { getExcelObjs } = require('../utils/uploadExcelSrv')
const { exec } = require('../db/index')
router.prefix('/api')

const homeControl = require('../controller/home')

router.post('/upload', homeControl.upload)

router.post('/ufile', async (ctx, next) => {
  let data = await getExcelObjs(ctx)
  let fdata = data.datas[0].filter(v => v.__EMPTY_1 === '上海网站工程学院')
  fdata.forEach(async (item, index) => {
    await exec(`insert into wz_grade (userid, username, password, theory, skill, date) values ('${item.__EMPTY_7}', '${item.__EMPTY_8}','${123}', '${item.__EMPTY_11}','${item.__EMPTY_12}','${item.__EMPTY_9}')`)
  })
  ctx.body = {
    fdata
  }
})

router.get('/wz_grade', async (ctx, next) => {
  let { userid } = ctx.query;
  let data = await exec(`select userid, date, username, theory, skill from wz_grade where userid='${userid}'`)
  ctx.body = {
    data
  }
})



module.exports = router;